package com.winter.portal.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.winter.portal.api.dto.EnabledDTO;
import com.winter.portal.api.dto.RoleDTO;
import com.winter.portal.api.dto.RoleResourceDTO;
import com.winter.portal.api.query.RoleQuery;
import com.winter.portal.api.vo.RoleResourceVO;
import com.winter.portal.api.vo.RoleVO;
import com.winter.portal.server.constant.Constants;
import com.winter.portal.server.entity.RoleEntity;
import com.winter.portal.server.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jzyan.framework.core.response.CommonRes;
import org.jzyan.framework.core.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 角色
 * </p>
 *
 * @author jzyan
 * @since 2023-02-02
 */
@Validated
@RestController
@RequestMapping(Constants.ADMIN_PREFIX + "/role")
@Api(value = "角色接口", tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("table")
    @ApiOperation(value = "角色列表", tags = "角色管理")
    public CommonRes<IPage<RoleVO>> table(RoleQuery roleQuery) {
        return CommonRes.success(roleService.table(roleQuery));
    }

    @GetMapping
    @ApiOperation(value = "角色详情", tags = "角色管理")
    public CommonRes<RoleVO> detail(@RequestParam Serializable id) {
        RoleEntity roleEntity = roleService.getById(id);
        return CommonRes.success(ConvertUtil.convert(roleEntity, RoleVO.class));
    }

    @PostMapping
    @ApiOperation(value = "添加角色", tags = "角色管理")
    public CommonRes add(@RequestBody @Valid RoleDTO role) {
        roleService.add(role);
        return CommonRes.success();
    }

    @PutMapping
    @ApiOperation(value = "更新角色", tags = "角色管理")
    public CommonRes update(@RequestBody @Validated(value = {Default.class, RoleDTO.Update.class}) RoleDTO role) {
        RoleEntity roleEntity = ConvertUtil.convert(role, RoleEntity.class);
        roleService.update(roleEntity);
        return CommonRes.success();
    }

    @PutMapping("enabled")
    @ApiOperation(value = "更新角色状态", tags = "角色管理")
    public CommonRes updateStatus(@RequestBody @Valid EnabledDTO enabled) {
        RoleEntity roleEntity = ConvertUtil.convert(enabled, RoleEntity.class);
        roleService.update(roleEntity);
        return CommonRes.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除角色", tags = "角色管理")
    public CommonRes delete(Serializable id) {
        roleService.removeById(id);
        return CommonRes.success();
    }

    @GetMapping("resource")
    @ApiOperation(value = "获取资源列表", tags = "角色管理")
    public CommonRes<List<RoleResourceVO>> getResource(@RequestParam Serializable id) {
        return CommonRes.success(roleService.getResource(id));
    }

    @PostMapping("resource")
    @ApiOperation(value = "添加角色资源", tags = "角色管理")
    public CommonRes addResource(@RequestBody @Valid RoleResourceDTO roleResource) {
        roleService.addResource(roleResource);
        return CommonRes.success();
    }

}

